package com.uphone.kingmall.activity.personal.set;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.DeviceUtil;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.PhoneFormatCheckUtils;
import com.uphone.kingmall.utils.SharedPreferencesHelper;
import com.uphone.kingmall.utils.ToastUtils;
import com.uphone.kingmall.view.SubmitButton;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <pre>
 *     author : 王雨
 *     e-mail : sunzengyan@outlook.com
 *     time   : 2018/07/31
 *     desc   :更换手机号
 *     version: 1.0
 * </pre>
 */
public class ChangePhoneActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_get_yzm)
    TextView tvGetYzm;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_bind)
    SubmitButton btnBind;
    private String phone;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_change_phone;
    }

    @Override
    public void initView() {
        setStatusBar();
        btnBind.setRelaViews(etPhone, etCode);
    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.iv_back, R.id.tv_get_yzm, R.id.btn_bind})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_get_yzm:
                getMachCode();
                break;
            case R.id.btn_bind:
                if (TextUtils.isEmpty(etPhone.getText().toString().trim()) || TextUtils.isEmpty(etCode.getText().toString().trim())) {
                    showShortToast("请将信息填写完整");
                    return;
                }
                if (!PhoneFormatCheckUtils.isPhoneLegal(etPhone.getText().toString())) {
                    showShortToast("请输入正确的手机号");
                    return;
                }
                HttpParams params = new HttpParams();
                phone = etPhone.getText().toString().trim();
                params.put("mobile", phone);
                params.put("code", etCode.getText().toString().trim());
                OkGoUtils.progressRequest(MyUrl.bindMobile, this, params, new onNormalRequestListener() {
                    @Override
                    public void onSuccess(String response, int code) {
                        if (code == 0) {
                            SharedPreferencesHelper.saveUserPhone(phone);
                            ToastUtils.showShortToast(ChangePhoneActivity.this, "换绑成功！");
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    finish();
                                }
                            }, 500);
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {

                    }
                });
                break;
        }
    }

    private void getMachCode() {

        if (TextUtils.isEmpty(etPhone.getText().toString())) {
            showShortToast("手机号不能为空");
            return;
        }
        if (!PhoneFormatCheckUtils.isPhoneLegal(etPhone.getText().toString())) {
            showShortToast("手机号不正确");
            return;
        }
        DeviceUtil.sendSmsCode(this, etPhone.getText().toString().trim(), "3", new DeviceUtil.DeviceCodeCallBack() {
            @Override
            public void callBack(String deviceCode) {
                timer.start();
                tvGetYzm.setEnabled(false);
                tvGetYzm.setTextColor(Color.parseColor("#cccccc"));
            }
        });

    }

    /**
     * 倒计时
     */
    CountDownTimer timer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long l) {
            tvGetYzm.setText(l / 1000 + "s");
        }

        @Override
        public void onFinish() {
            tvGetYzm.setEnabled(true);
            tvGetYzm.setText("重新获取");
            tvGetYzm.setTextColor(Color.parseColor("#E33344"));
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}
